/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.profiles.PropertyComposite;
import com.cea.papyrus.core.profiles.StereotypeComposite;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;
import com.cea.papyrus.profile.tree.objects.RootElementTreeObject;

//TODO: Auto-generated Javadoc
/**
 * The Class StereotypeSection.
 */
public class StereotypeSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private StereotypeComposite stereotypeComposite;
	
	/**
	 * 
	 */
	private PropertyComposite propertyComposite;
	
	/**
	 * 
	 */
	private ModelManager modelManager;

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		stereotypeComposite = new StereotypeComposite(parent);
		stereotypeComposite.createContent(parent, getWidgetFactory());
		
		propertyComposite = new PropertyComposite(parent, stereotypeComposite);
		propertyComposite.createContent(parent, getWidgetFactory());
		
		stereotypeComposite.setPropertyComposite(propertyComposite);
	}

	/**
	 * 
	 */
	@Override
	public void refresh() {
		stereotypeComposite.refresh();
		propertyComposite.refresh();
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public boolean shouldUseExtraSpace() { 
		return true; 
	}

	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		
		if(input instanceof IUMLElementEditPart) {
			this.modelManager = ((IUMLElementEditPart) input).getModelManager();
			stereotypeComposite.setSelection(selection);
			stereotypeComposite.setElement(((IUMLElementEditPart) input).getUmlElement());
			stereotypeComposite.setInput(new RootElementTreeObject(((IUMLElementEditPart) input).getUmlElement()));
			stereotypeComposite.setModelManager(this.modelManager);
			
			if((input instanceof GraphNodeEditPart) || (input instanceof GraphEdgeEditPart)) {
				diagramElement = (DiagramElement)((AbstractGraphicalEditPart)input).getModel();
			} else {
				// re-init the diagram element. Else, could cause a bug,
				// when the user selects a diagram element, then a non diagram element.
				// If display button is pressed, the "Toggle Display" button does not work correctly
				diagramElement = null;
			}
			stereotypeComposite.setDiagramElement(diagramElement);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(stereotypeComposite!=null)
			stereotypeComposite.disposeListeners();
		if(propertyComposite!=null)
			propertyComposite.disposeListeners();
	}
}